package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.DataDictionaryItem;
import cn.wolfcode.crm.domain.Menu;
import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.domain.DataDictionary;
import cn.wolfcode.crm.mapper.DataDictionaryItemMapper;
import cn.wolfcode.crm.mapper.DataDictionaryMapper;
import cn.wolfcode.crm.service.IDataDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class DataDictionaryServiceImpl implements IDataDictionaryService {
    @Autowired
    private DataDictionaryMapper dataDictionaryMapper;
    @Autowired
    private DataDictionaryItemMapper dataDictionaryItemMapper;


    public List<DataDictionary> selectAll() {
        return dataDictionaryMapper.selectAll();
    }

    public Boolean selectByName(Long id, String name) {
        DataDictionary dataDictionary = dataDictionaryMapper.selectByName(name);
        return dataDictionary == null || dataDictionary.getId() == id;
    }

    public void saveOrUpdate(DataDictionary dataDictionary) {
        if (dataDictionary.getId() == null) {
            dataDictionaryMapper.insert(dataDictionary);
        } else {
            dataDictionaryMapper.updateByPrimaryKey(dataDictionary);
        }
    }

    public void delete(Long id) {
        if (id != null) {
            dataDictionaryItemMapper.deleteByParentId(id);
            dataDictionaryMapper.deleteByPrimaryKey(id);
        }
    }

    public List<Map<String, String>> getAllLevel(String level) {
        Long levelId = dataDictionaryMapper.getLevelID(level);
        return dataDictionaryItemMapper.selectAllLevel(levelId);
    }
}
